import React from 'react'
import { Route, Redirect } from 'react-router-dom'
import { useAuth } from '../context/AuthProvider'

const AuthRoute = ({authCheckType,component:Component,routes,...rest}) => {
    const {user} = useAuth();
    
    if(authCheckType=='authed'&&!Object.keys(user).length){
        return <Redirect to='/login' />
    }
    if(authCheckType=='unauthed'&&!!Object.keys(user).length){
        return <Redirect to='/' />
    }
    return (
        <Route 
            render={
                (props)=><Component {...props} routes={routes} />
            }
            {...rest} 
        />
    )
}

export default AuthRoute